const express = require('express');
const router = express.Router();
const Blog = require('../../mongoModuels/rear/blog');

router
.get('/article', (req, res) => {
    if(req.query.pageSize && req.query.pageNum) {
        let data = {
            pageSize: parseInt(req.query.pageSize),
            pageNum: parseInt(req.query.pageNum),
            keyWord: req.query.keyWord
        }
        let title = new RegExp(data.keyWord, 'i');
        let isKeyWord = {'$or': [{'title': title}, {'label': title}]};
        Blog.article.count(isKeyWord).then(count => {
            let query = Blog.article.find(isKeyWord);
            query.skip((data.pageNum - 1) * data.pageSize).limit(data.pageSize);
            query.exec({}).then(rows => {
				rows.forEach(item => {
					if(!item.browse) {
						item.browse = 0;
					}
				})
                res.json({
                    status: 1,
                    message: '操作成功',
                    data: {
                        rows,
                        count
                    }
                })
            }).catch(() => {
                res.json({
                    status: 0,
                    message: '操作失败'
                })
            })
        }).catch(() => {
            res.json({
                status: 0,
                message: '操作失败'
            })
        })
    } else {
        res.json({
            status: 0,
            message: '参数不足'
        })
    }
})
//查看文章

.post('/prohibit/article', (req, res) => {
    if(req.body.id) {
        Blog.article.updateOne({'_id': req.body.id}, {type: 2}).then(rows => {
            res.json({
                status: 1,
                message: '禁用成功'
            })
        }).catch(() => {
            res.json({
                status: 0,
                message: '禁用失败'
            })
        })
    } else {
        res.json({
            status: 0,
            message: '参数不足'
        })
    }
})
//文章禁用

.post('/enable/article', (req, res) => {
    if(req.body.id) {
        Blog.article.updateOne({'_id': req.body.id}, {type: 1}).then(rows => {
            res.json({
                status: 1,
                message: '启用成功'
            })
        }).catch(() => {
            res.json({
                status: 0,
                message: '启用失败'
            })
        })
    } else {
        res.json({
            status: 0,
            message: '参数不足'
        })
    }
})
//文章启用

.post('/delete/article', (req, res) => {
    if(req.body.id) {
        Blog.article.remove({'_id': req.body.id}).then(() => {
            res.json({
                status: 1,
                message: '删除成功'
            })
        }).catch(() => {
            res.json({
                status: 0,
                message: '删除失败'
            })
        })
    } else {
        res.json({
            status: 0,
            message: '参数不足'
        })
    }
})
// 删除文章

.get('/article/mag', (req, res) => {
    if(req.query.id && req.query.pageSize && req.query.pageNum) {
        let date = {
            id: req.query.id,
            pageSize: parseInt(req.query.pageSize),
            pageNum: parseInt(req.query.pageNum),
            keyWord: req.query.keyWord
        }
        let query = Blog.article.findOne({'_id': date.id});
        query.exec({}, (err, data) => {
            if(err) {
                res.json({
                    status: 0,
                    message: '操作失败'
                })
            } else {
                getMas(date, data, res)
            }
        })
    } else {
        res.json({
            status: 0,
            message: '参数不全'
        })
    }
})
// 查看评论

.post('/prohibit/mag', (req, res) => {
    if(req.body.id) {
        if(req.body.curId) {
            Blog.articleMas.findOne({'_id': req.body.id}).then(data => {
                let rows = [];
                data.mag.forEach(item => {
                    if(item._id === req.body.curId) {
                        item.type = 2;
                    }
                    rows.push(item);
                });
                Blog.articleMas.updateOne({'_id': req.body.id}, {'mag': rows}).then(() => {
                    res.json({
                        status: 1,
                        message: '操作成功'
                    })
                }).catch(() => {
                    res.json({
                        status: 0,
                        message: '操作失败'
                    })
                })
            }).catch(() => {
                res.json({
                    status: 0,
                    message: '操作失败'
                })
            })
        } else {
            Blog.articleMas.updateOne({'_id': req.body.id}, {'type': 2}).then(() => {
                res.json({
                    status: 1,
                    message: '操作成功'
                })
            }).catch(() => {
                res.json({
                    status: 0,
                    message: '操作失败'
                })
            })
        }
    } else {
        res.json({
            status: 0,
            message: '参数不足'
        })
    }
})
//评论禁用

.post('/enable/mag', (req, res) => {
    if(req.body.id) {
        if(req.body.curId) {
            Blog.articleMas.findOne({'_id': req.body.id}).then(data => {
                let rows = [];
                data.mag.forEach(item => {
                    if(item._id === req.body.curId) {
                        item.type = 1;
                    }
                    rows.push(item);
                });
                Blog.articleMas.updateOne({'_id': req.body.id}, {'mag': rows}).then(() => {
                    res.json({
                        status: 1,
                        message: '操作成功'
                    })
                }).catch(() => {
                    res.json({
                        status: 0,
                        message: '操作失败'
                    })
                })
            }).catch(() => {
                res.json({
                    status: 0,
                    message: '操作失败'
                })
            })
        } else {
            Blog.articleMas.updateOne({'_id': req.body.id}, {'type': 1}).then(() => {
                res.json({
                    status: 1,
                    message: '操作成功'
                })
            }).catch(() => {
                res.json({
                    status: 0,
                    message: '操作失败'
                })
            })
        }
    } else {
        res.json({
            status: 0,
            message: '参数不足'
        })
    }
})
//评论启用

.post('/delete/mag', (req, res) => {
    if(req.body.id) {
        if(req.body.curId) {
            Blog.articleMas.findOne({'_id': req.body.id}).then(data => {
                let rows = [];
                data.mag.forEach((item, i, data) => {
                    if(item._id === req.body.curId) {
                        data.splice(i, 1);
                        rows = data;
                    }
                });
                Blog.articleMas.updateOne({'_id': req.body.id}, {'mag': rows}).then(() => {
                    res.json({
                        status: 1,
                        message: '操作成功'
                    })
                }).catch(() => {
                    res.json({
                        status: 0,
                        message: '操作失败'
                    })
                })
            }).catch(() => {
                res.json({
                    status: 0,
                    message: '操作失败'
                })
            })
        } else {
            Blog.articleMas.remove({'_id': req.body.id}).then(() => {
                res.json({
                    status: 1,
                    message: '操作成功'
                })
            }).catch(() => {
                res.json({
                    status: 0,
                    message: '操作失败'
                })
            })
        }
    } else {
        res.json({
            status: 0,
            message: '参数不足'
        })
    }
})
//删除评论

const getMas = (date, data, res) => {
    let title = new RegExp(date.keyWord, 'i');
    let isKeyWord = {'_id': {$in: data.mag}, '$or': [{'name': title}]};
    Blog.articleMas.count(isKeyWord).then(count => {
        let query = Blog.articleMas.find(isKeyWord);
        query.skip((date.pageNum - 1) * date.pageSize).limit(date.pageSize);
        query.exec({}).then(rows => {
            res.json({
                status: 1,
                message: '操作成功',
                rows: {
                    rows,
                    count
                }
            })
        }).catch(() => {
            res.json({
                status: 0,
                message: '操作失败'
            })
        })
    }).catch(() => {
        res.json({
            status: 0,
            message: '操作失败'
        })
    })
}
//查看评论

module.exports = router;